import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/student/assignmentSearchDto.dart';
import 'package:ichinesereader/pages/common/report/reportStudentPersonalActivity.dart';
import 'package:ichinesereader/pages/common/report/reportStudentPersonalAssessment.dart';
import 'package:ichinesereader/pages/common/report/reportStudentPersonalHomework.dart';
import 'package:ichinesereader/pages/common/report/reportsStudentPersonalLevel.dart';
import 'package:ichinesereader/pages/common/report/reportsStudentPersonalOverview.dart';
import 'package:ichinesereader/pages/common/report/reportsStudentPersonalSelfReading.dart';
import 'package:ichinesereader/pages/common/report/reportsStudentPersonalSkill.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:intl/intl.dart';
import '../../../themes/appTheme.dart';

String studentPersonalReportTab = Constants.studentPersonalReportTabs[0];
GlobalKey<ReportStudentPersonalOverviewState> reportStudentPersonalOverviewKey =
    GlobalKey<ReportStudentPersonalOverviewState>();
GlobalKey<ReportsStudentPersonalSkillState> reportStudentPersonalSkillsKey =
    GlobalKey<ReportsStudentPersonalSkillState>();
GlobalKey<ReportsStudentPersonalActivityState>
    reportStudentPersonalActivityKey =
    GlobalKey<ReportsStudentPersonalActivityState>();
GlobalKey<ReportsStudentPersonalSelfReadingState>
    reportsStudentPersonalSelfReadingKey =
    GlobalKey<ReportsStudentPersonalSelfReadingState>();
GlobalKey<ReportsStudentPersonalLevelState> reportsStudentPersonalLevelKey =
    GlobalKey<ReportsStudentPersonalLevelState>();
GlobalKey<ReportStudentPersonalAssessmentState>
    reportsStudentPersonalAssessmentKey =
    GlobalKey<ReportStudentPersonalAssessmentState>();
GlobalKey<ReportStudentPersonalAssignmentState>
    reportsStudentPersonalAssignmentKey =
    GlobalKey<ReportStudentPersonalAssignmentState>();

class ReportStudentPersonalContent extends StatefulWidget {
  ReportStudentPersonalContent({
    Key? key,
    required this.userName,
    required this.classId,
  }) : super(key: key);
  final String userName;
  final String classId;

  _ReportStudentPersonalContent createState() =>
      new _ReportStudentPersonalContent();
}

class _ReportStudentPersonalContent extends State<ReportStudentPersonalContent>
    with TickerProviderStateMixin {
  late TabController _tabController;
  final ReportsController _reportsController = Get.put(ReportsController());
  final box = Hive.box('appconfig');

  AssignmentSearchDto dto = new AssignmentSearchDto(
      assignmentType: "ASSIGNMENT",
      live: true,
      past: false,
      sortField: "-created");

  @override
  void initState() {
    studentPersonalReportTab = Constants.studentPersonalReportTabs[0];

    bool isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    _tabController = TabController(
      length: isParent ? 6 : 7,
      vsync: this,
    );

    _reportsController.fetchClassStudentReportByClass(
        widget.classId, widget.userName, dto);

    super.initState();
  }

  Widget build(BuildContext context) {
    Log.debug('build reportsStudentPersonal content');
    bool isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    var filteredtabs = isParent
        ? Constants.studentPersonalReportTabs.where((ele) => ele != 'Skills')
        : Constants.studentPersonalReportTabs;
    return Column(children: [
      _reportsController.obx((data) {
        var student = _reportsController.classstudentreport.value.student;
        if (student != null) {
          return Container(
              margin: EdgeInsets.only(top: 6.0),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(3.0),
                  color: Get.isDarkMode
                      ? AppTheme.colors.darkTheme2
                      : Colors.white.withOpacity(0.2)),
              padding: const EdgeInsets.all(21.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  //Profile Area
                  Stack(
                    alignment: Alignment.topCenter,
                    children: [
                      Container(
                        width: 96,
                        height: 96,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(18.0),
                          color: Get.isDarkMode
                              ? Colors.white.withOpacity(0.1)
                              : Colors.black.withOpacity(0.1),
                        ),
                      ),
                      Container(
                        width: 90,
                        height: 90,
                        child: student.profileImageUrl != null
                            ? Image.network(
                                student.profileImageUrl!,
                              )
                            : Image.asset(
                                'assets/images/profileAvartar.png',
                              ),
                      ),
                    ],
                  ),
                  Expanded(
                      child: Container(
                    margin: EdgeInsets.only(left: 18.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Text(
                          student.firstName! + ' ' + student.lastName!,
                          style: Theme.of(context)
                              .textTheme
                              .displayLarge
                              ?.copyWith(
                                color: Get.isDarkMode
                                    ? Get.theme.primaryColorDark
                                    : Get.theme.primaryColorLight,
                                fontSize: 21.0,
                              ),
                        ),
                        Row(
                          children: [
                            Text(
                              "Username:".tr,
                              style: Theme.of(context)
                                  .textTheme
                                  .titleSmall
                                  ?.copyWith(
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black.withOpacity(0.6),
                                    fontSize: 15.0,
                                  ),
                            ),
                            SizedBox(width: 6.0),
                            Text(
                              student.userName!,
                              style: Theme.of(context)
                                  .textTheme
                                  .titleSmall
                                  ?.copyWith(
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black.withOpacity(0.6),
                                    fontSize: 15.0,
                                    fontWeight: FontWeight.bold,
                                  ),
                            )
                          ],
                        ),
                        Row(
                          children: [
                            Text(
                              "Points Earned:".tr,
                              style: Theme.of(context)
                                  .textTheme
                                  .titleSmall
                                  ?.copyWith(
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black.withOpacity(0.6),
                                    fontSize: 15.0,
                                  ),
                            ),
                            SizedBox(width: 6.0),
                            Text(
                              student.points!.toString(),
                              style: Theme.of(context)
                                  .textTheme
                                  .titleSmall
                                  ?.copyWith(
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black.withOpacity(0.6),
                                    fontSize: 15.0,
                                    fontWeight: FontWeight.bold,
                                  ),
                            ),
                          ],
                        ),
                        Row(
                          children: [
                            Text(
                              "Last Login:".tr,
                              style: Theme.of(context)
                                  .textTheme
                                  .titleSmall
                                  ?.copyWith(
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black.withOpacity(0.6),
                                    fontSize: 15.0,
                                  ),
                            ),
                            SizedBox(width: 6.0),
                            Text(
                              student.lastLoggedIn! > 0
                                  ? DateFormat('MM/dd/yyyy kk:mm').format(
                                      DateTime.fromMillisecondsSinceEpoch(
                                          student.lastLoggedIn!))
                                  : '--',
                              style: Theme.of(context)
                                  .textTheme
                                  .titleSmall
                                  ?.copyWith(
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black.withOpacity(0.6),
                                    fontSize: 15.0,
                                    fontWeight: FontWeight.bold,
                                  ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ))
                ],
              ));
        } else {
          return Text('');
        }
      }),
      Container(
        width: double.infinity,
        margin: EdgeInsets.only(top: 12.0),
        padding: EdgeInsets.all(12.0),
        decoration: BoxDecoration(
          color: Get.isDarkMode
              ? AppTheme.colors.darkTheme2
              : Colors.black.withOpacity(0.1),
          borderRadius: BorderRadius.circular(3.0),
        ),
        child: TabBar(
          labelColor: Get.isDarkMode
              ? Get.theme.primaryColorDark
              : Get.theme.primaryColorLight,
          //unselectedLabelColor: Get.isDarkMode ? Get.theme.primaryColorDark : null,
          indicator: BoxDecoration(
            color: Get.isDarkMode ? Colors.black : AppTheme.colors.purpleBtn,
            borderRadius: BorderRadius.circular(3.0),
            boxShadow: Get.isDarkMode
                ? null
                : [
                    BoxShadow(
                      blurRadius: 3,
                      spreadRadius: 1,
                      color: Get.isDarkMode
                          ? Colors.transparent
                          : Colors.black.withOpacity(0.1),
                      offset: Offset(1, 1),
                    ),
                  ],
          ),
          isScrollable: true,
          labelPadding:
              const EdgeInsets.symmetric(vertical: 3.0, horizontal: 15.0),
          labelStyle: Theme.of(context).textTheme.titleSmall,
          tabs: filteredtabs
              .map((e) => Text(
                    e.tr,
                    style: TextStyle(
                        color: Get.isDarkMode
                            ? Get.theme.primaryColorDark
                            : Get.theme.primaryColorLight),
                  ))
              .toList(),
          controller: _tabController,
        ),
      ),
      Expanded(
        child: TabBarView(
          controller: _tabController,
          children: isParent
              ? [
                  ReportStudentPersonalOverview(
                    key: reportStudentPersonalOverviewKey,
                    userName: widget.userName,
                  ),
                  ReportsStudentPersonalActivity(
                      key: reportStudentPersonalActivityKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportsStudentPersonalSelfReading(
                      key: reportsStudentPersonalSelfReadingKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportStudentPersonalHomework(
                      key: reportsStudentPersonalAssignmentKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportStudentPersonalAssessment(
                      key: reportsStudentPersonalAssessmentKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportsStudentPersonalLevel(
                      key: reportsStudentPersonalLevelKey,
                      userName: widget.userName)
                ]
              : [
                  ReportStudentPersonalOverview(
                    key: reportStudentPersonalOverviewKey,
                    userName: widget.userName,
                  ),
                  ReportsStudentPersonalSkill(
                      key: reportStudentPersonalSkillsKey,
                      userName: widget.userName),
                  ReportsStudentPersonalActivity(
                      key: reportStudentPersonalActivityKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportsStudentPersonalSelfReading(
                      key: reportsStudentPersonalSelfReadingKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportStudentPersonalHomework(
                      key: reportsStudentPersonalAssignmentKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportStudentPersonalAssessment(
                      key: reportsStudentPersonalAssessmentKey,
                      classId: widget.classId,
                      userName: widget.userName),
                  ReportsStudentPersonalLevel(
                      key: reportsStudentPersonalLevelKey,
                      userName: widget.userName)
                ],
        ),
      ),
    ]);
  }
}
